package org.celery.modules.other.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.celery.modules.other.excel.SevenElevenShopExcel;
import org.celery.modules.other.excel.SevenElevenShopImportListener;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.celery.modules.other.entity.SevenElevenShop;
import org.celery.modules.other.vo.SevenElevenShopVO;
import org.celery.modules.other.wrapper.SevenElevenShopWrapper;
import org.celery.modules.other.service.ISevenElevenShopService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 7-11商超表 控制器
 *
 * @author Celery
 * @since 2022-06-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("other/seven-eleven-shop")
@Api(value = "7-11商超表", tags = "7-11商超表接口")
public class SevenElevenShopController extends BladeController {

	private final ISevenElevenShopService sevenElevenShopService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入sevenElevenShop")
	public R<SevenElevenShopVO> detail(SevenElevenShop sevenElevenShop) {
		SevenElevenShop detail = sevenElevenShopService.getOne(Condition.getQueryWrapper(sevenElevenShop));
		return R.data(SevenElevenShopWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 7-11商超表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入sevenElevenShop")
	public R<IPage<SevenElevenShopVO>> list(SevenElevenShop sevenElevenShop, Query query) {
		QueryWrapper<SevenElevenShop> queryWrapper = Condition.getQueryWrapper(sevenElevenShop);
		queryWrapper.lambda().orderByAsc(SevenElevenShop::getSerial);
		if (Func.isNotEmpty(sevenElevenShop.getAddress())) {
			queryWrapper.lambda().like(SevenElevenShop::getAddress, sevenElevenShop.getAddress());
			sevenElevenShop.setAddress(null);
		}
		IPage<SevenElevenShop> pages = sevenElevenShopService.page(Condition.getPage(query), queryWrapper);
		return R.data(SevenElevenShopWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 7-11商超表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入sevenElevenShop")
	public R<IPage<SevenElevenShopVO>> page(SevenElevenShopVO sevenElevenShop, Query query) {
		IPage<SevenElevenShopVO> pages = sevenElevenShopService.selectSevenElevenShopPage(Condition.getPage(query), sevenElevenShop);
		return R.data(pages);
	}

	/**
	 * 新增 7-11商超表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入sevenElevenShop")
	public R save(@Valid @RequestBody SevenElevenShop sevenElevenShop) {
		return R.status(sevenElevenShopService.save(sevenElevenShop));
	}

	/**
	 * 修改 7-11商超表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入sevenElevenShop")
	public R update(@Valid @RequestBody SevenElevenShop sevenElevenShop) {
		return R.status(sevenElevenShopService.updateById(sevenElevenShop));
	}

	/**
	 * 新增或修改 7-11商超表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入sevenElevenShop")
	public R submit(@Valid @RequestBody SevenElevenShop sevenElevenShop) {
		return R.status(sevenElevenShopService.saveOrUpdate(sevenElevenShop));
	}

	
	/**
	 * 删除 7-11商超表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(sevenElevenShopService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导入数据
	 */
	@PostMapping("import-data")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "导入数据", notes = "传入excel")
	public R importData(MultipartFile file, Integer isCovered) {
		String filename = file.getOriginalFilename();
		if (StringUtils.isEmpty(filename)) {
			throw new RuntimeException("请上传文件!");
		}
		if ((!StringUtils.endsWithIgnoreCase(filename, ".xls") && !StringUtils.endsWithIgnoreCase(filename, ".xlsx"))) {
			throw new RuntimeException("请上传正确的excel文件!");
		}
		InputStream inputStream;
		try {
			SevenElevenShopImportListener importListener = new SevenElevenShopImportListener(sevenElevenShopService);
			inputStream = new BufferedInputStream(file.getInputStream());
			ExcelReaderBuilder builder = EasyExcel.read(inputStream, SevenElevenShopExcel.class, importListener);
			builder.doReadAll();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return R.success("操作成功");
	}

	
}
